
function header = parseProcessedHeader(headerLines)
%PARSEPROCESSEDHEADER Parses the data contained in the header added by SBE
% Data Processing. This includes the column layout of the data in the .cnv 
% file. 
%
% Inputs:
%   headerLines - Cell array of strings, the lines in the processed header 
%                 section.
%
% Outputs:
%   header      - struct containing information that was contained in the
%                 processed header section.
%

  header = struct;
  header.columns = {};
  
  nameExpr = 'name \d+ = (.+):';
  nvalExpr = 'nvalues = (\d+)';
  badExpr  = 'bad_flag = (.*)$';
  %BDM (18/02/2011) - added to get start time
  startExpr = 'start_time = (\w+ \d+ \d+ \d+:\d+:\d+)';
  volt0Expr = 'sensor \d+ = Extrnl Volt  0  (.+)';
  volt1Expr = 'sensor \d+ = Extrnl Volt  1  (.+)';
  volt2Expr = 'sensor \d+ = Extrnl Volt  2  (.+)';
  binExpr   = 'binavg_binsize = (\d+)';
  
  for k = 1:length(headerLines)
    
    % try name expr
    tkns = regexp(headerLines{k}, nameExpr, 'tokens');
    if ~isempty(tkns)
      header.columns{end+1} = tkns{1}{1};
      continue; 
    end
    
    % then try nvalues expr
    tkns = regexp(headerLines{k}, nvalExpr, 'tokens');
    if ~isempty(tkns)
      header.nValues = str2double(tkns{1}{1});
      continue;
    end
    
    % then try bad flag expr
    tkns = regexp(headerLines{k}, badExpr, 'tokens');
    if ~isempty(tkns)
      header.badFlag = str2double(tkns{1}{1});
      continue;
    end
    
    %BDM (18/02/2011) - added to get start time
    % then try startTime expr
    tkns = regexp(headerLines{k}, startExpr, 'tokens');
    if ~isempty(tkns)
      header.startTime = datenum(tkns{1}{1}, 'mmm dd yyyy HH:MM:SS');
      continue;
    end
    
    % then try volt exprs
    tkns = regexp(headerLines{k}, volt0Expr, 'tokens');
    if ~isempty(tkns)
      header.volt0Expr = tkns{1}{1};
      continue;
    end
    tkns = regexp(headerLines{k}, volt1Expr, 'tokens');
    if ~isempty(tkns)
      header.volt1Expr = tkns{1}{1};
      continue;
    end
    tkns = regexp(headerLines{k}, volt2Expr, 'tokens');
    if ~isempty(tkns)
      header.volt2Expr = tkns{1}{1};
      continue;
    end
    
    % then try bin expr
    tkns = regexp(headerLines{k}, binExpr, 'tokens');
    if ~isempty(tkns)
      header.binSize = str2double(tkns{1}{1});
      continue;
    end
  end
end

function time = genTimestamps(instHeader, data)
%GENTIMESTAMPS Generates timestamps for the data. Horribly ugly. I shouldn't 
% have to have a function like this, but the .cnv files do not necessarily 
% provide timestamps for each sample.
%
  
  % time may have been present in the sample 
  % data - if so, we don't have to do any work
  if isfield(data, 'TIME')
      time = data.TIME;
      return;
  end
  
  % To generate timestamps for the CTD data, we need to know:
  %   - start time
  %   - sample interval
  %   - number of samples
  %
  % The SBE19 header information does not necessarily provide all, or any
  % of this information. .
  %
  start    = 0;
  interval = 0.25;
    
  % figure out number of samples by peeking at the 
  % number of values in the first column of 'data'
  f = fieldnames(data);
  nSamples = length(data.(f{1}));
  
  % try and find a start date - use castDate if present
  if isfield(instHeader, 'castDate')
    start = instHeader.castDate;
  end
  
  % if castStart is present then it means we have several cast records
  if isfield(instHeader, 'castStart')
      time = NaN(1, instHeader.castEnd(end));
      for i=1:length(instHeader.castNumber)
          for j=instHeader.castStart(i):instHeader.castEnd(i)
              time(j) = instHeader.castDate(i) + (j-instHeader.castStart(i))*instHeader.castAvg(i)/(3600*24);
          end
      end
      return;
  end
  
  % if scanAvg field is present, use it to determine the interval
  if isfield(instHeader, 'scanAvg')
    interval = (0.25 * instHeader.scanAvg) / 86400;
  end
  
  % if one of the columns is 'Scan Count', use the 
  % scan count number as the basis for the timestamps 
  if isfield(data, 'ScanCount')
    time = ((data.ScanCount - 1) ./ 345600) + cStart;
  % if scan count is not present, calculate the 
  % timestamps from start, end and interval
  else
    time = (start:interval:start + (nSamples - 1) * interval)';
  end
end




